
NXGestureCaptureType = 
{
	NONE: 0,
	SWIPE_UP: 1,
	SWIPE_RIGHT: 2,
	SWIPE_DOWN: 3,
	SWIPE_LEFT: 4
}

var NXGestureCapture = function( eventTarget )
{
	var _prePosition;
	var _this = this;
	var _isLockV = true;
	var _isLockH = true;
	var _sensorPixel = 10;
	var _sx;
	var _sy;
	eventTarget.addEventListener( "touchstart", onTouchStart, false );
	eventTarget.addEventListener( "mousedown", onTouchStart, false );
	
	// 시작
	function onTouchStart( e )
	{
		var target;
		
		_isLockV = true;
		_isLockH = true;
		
		e.preventDefault();
		if( e.targetTouches )
		{
			target = e.targetTouches[0];
			document.addEventListener("touchmove", onMove, true );
			document.addEventListener("touchend", onTouchEnd, true);
		}
		else
		{
			target = e;	
			document.addEventListener("mousemove", onMove, true );
			document.addEventListener("mouseup", onTouchEnd, true);	
		}
		
		_vx = 0;
		_vy = 0;
		_prePosition = { x: target.pageX, y: target.pageY };
		
		_sx =  target.pageX;
		_sy =  target.pageY;
		
		if( _this.onStart != null )
		{
			_this.onStart( 0, 0 );
		}
	}
	
	// [ Call Back ] 각 제스쳐의 유형을 판별함
	// 		> function( vx, vy );
	//		> 현재는 vector X, vector Y
	this.onStart;
	this.onUpdate;
	// 		> function( vx, vy, type );
	//		> 현재는 vector X, vector Y, geture 유형( NXGestureCaptureType )
	this.onEnd;
	
	var _vx = 0;
	var _vy = 0;
	
	function onMove( e )
	{
		var target;
		e.preventDefault();
		if( e.targetTouches )
		{
			target = e.targetTouches[0];	
		}
		else
		{
			target = e;	
		}
		
		if( _isLockV == true && _isLockH == true )
		{
			// 종단제스쳐, 상하제스쳐 판단
			var _dx = Math.abs( _sx - target.pageX );
			var _dy = Math.abs( _sy - target.pageY );
			_isLockH = ( _dx < _sensorPixel );
			_isLockV = ( _dy < _sensorPixel );
			
			if( Math.sqrt( _dx*_dx+_dy*_dy ) < _sensorPixel )
			{
				_isLockH = _isLockV = true;
			}
			
			return;
		}
		
		_vx = parseFloat( target.pageX ) - parseFloat( _prePosition.x );
		_vy = parseFloat( target.pageY ) - parseFloat( _prePosition.y );
		_prePosition.x = target.pageX;
		_prePosition.y = target.pageY;
		
		if( _this.onUpdate != null )
		{
			if( _isLockH ) _vx = 0;
			if( _isLockV ) _vy = 0;
			_this.onUpdate( _vx, _vy );
		}
	}
	
	
	
	function onTouchEnd( e )
	{
		var target;
		e.preventDefault();
		if( e.targetTouches )
		{
			target = e.targetTouches[0];	
			document.removeEventListener("touchmove", onMove, true );
			document.removeEventListener("touchend", onTouchEnd, true);
		}
		else
		{
			target = e;	
			document.removeEventListener("mousemove", onMove, true );
			document.removeEventListener("mouseup", onTouchEnd, true);
		}
		
		if( _this.onEnd != null )
		{
			var _type = NXGestureCaptureType.NONE;
			
			// 좌/우 스와이핑 체크
			if( _vx < -_sensorPixel )
			{
				_type = NXGestureCaptureType.SWIPE_LEFT;
			}else if( _vx > _sensorPixel )
			{
				_type = NXGestureCaptureType.SWIPE_RIGHT;
			}
			/*
			if( Math.abs( _vy ) < Math.abs( _vx ) )
			{
				// 좌/우 스와이핑 체크
				if( _vx < -20 )
				{
					_type = NXGestureCaptureType.SWIPE_LEFT;
				}else if( _vx > 20 )
				{
					_type = NXGestureCaptureType.SWIPE_RIGHT;
				}
			}else if( Math.abs( _vx ) < Math.abs( _vy ) ){
				// 상하 스와이핑 체크
				if( _vy < -20 )
				{
					_type = NXGestureCaptureType.SWIPE_UP;
				}else if( _vy > 20 )
				{
					_type = NXGestureCaptureType.SWIPE_DOWN;
				}
			}
			*/
			_this.onEnd( _vx, _vy, _type );
		}
		_vx = 0;
		_vy = 0;
	}
}